package com.mobilesales.android.activities.common;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

import com.mobilesales.android.R;
import com.mobilesales.android.infrastructure.Language;
import com.mobilesales.android.infrastructure.SyncTask;
import com.mobilesales.android.util.Constants;
import com.mobilesales.android.util.MessageBox;
import com.mobilesales.android.util.Settings;

public class SettingsActivity extends Activity {

	private EditText usernameField;
	private EditText passwordField;
	private EditText serverField;
	private EditText c2dmServerMail;
	private CheckBox syncOnStartCb;
	private Spinner languageSpinner;
	ArrayAdapter<Language> langAdapter;
	boolean settingsChanged = false;
	
	
	private Settings currentSettings;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		updateGui();
		currentSettings = this.loadSettings();
	}
	
	public void updateGui() {
		setContentView(R.layout.settings);
		
		usernameField = (EditText)this.findViewById(R.id.userName);
		passwordField = (EditText)this.findViewById(R.id.password);
		c2dmServerMail = (EditText)this.findViewById(R.id.c2dmServerMail);
		serverField = (EditText)this.findViewById(R.id.serverAddress);
		syncOnStartCb = (CheckBox)this.findViewById(R.id.syncOnStartCb);
		languageSpinner = (Spinner)this.findViewById(R.id.languageSpinner);				
		langAdapter = new ArrayAdapter<Language>(this, R.layout.select_list_item, R.id.selectItemTxt, 
				Language.getAvailableLanguages(this));
		languageSpinner.setAdapter(langAdapter);
	}
	
	
	
	public void save(View v) {
		currentSettings = Settings.getSettings();
		String username = usernameField.getText().toString();
		String password = passwordField.getText().toString();
		String serverAddr = serverField.getText().toString();
		String c2dmSrvMail = c2dmServerMail.getText().toString();
		boolean syncOnStart = syncOnStartCb.isChecked();
		
		if (!validate()) {
			MessageBox.show(this, getString(R.string.ErrorValidate));
			return;
		}
		
		Language selectedLang = (Language)languageSpinner.getSelectedItem();
		
		if (currentSettings==null)
			currentSettings = new Settings();
		currentSettings.setLogin(username);
		currentSettings.setPassword(password);
		currentSettings.setServerUrl(serverAddr);
		currentSettings.setSyncOnStart(syncOnStart);
		currentSettings.setLanguage(selectedLang.getCode());
		currentSettings.setC2dmServerMail(c2dmSrvMail);
		
		if (saveSettings(currentSettings)) {
			Language.setCurrentLanguage(this, selectedLang.getCode());
			updateGui();
			this.loadSettings();
			settingsChanged = true;
			MessageBox.show(this, getString(R.string.SettingsSaved));
		}
		else {
			MessageBox.show(this, getString(R.string.ErrorSave));
		}
	}
	
	private boolean validate() {
		boolean valid = true;
		if (usernameField.getText().toString().equals("")) {
			usernameField.setError(getString(R.string.ErrorRequired));
			valid = false;
		}
		if (passwordField.getText().toString().equals("")) {
			passwordField.setError(getString(R.string.ErrorRequired));
			valid = false;
		}
		if (c2dmServerMail.getText().toString().equals("")) {
			c2dmServerMail.setError(getString(R.string.ErrorRequired));
			valid = false;
		}		
		if (serverField.getText().toString().equals("")) {
			serverField.setError(getString(R.string.ErrorRequired));
			valid = false;
		}
		return valid;	
	}

	@Override
	public void finish() {
		Bundle bundle = new Bundle();
		bundle.putBoolean(Constants.SettingsChanged, this.settingsChanged);
		Intent intent = new Intent();
		intent.putExtras(bundle);
		setResult(RESULT_OK, intent);		
		super.finish();
	}

	private boolean saveSettings(Settings settings) {
		try {
			settings.save();
			return true;
		} catch (IOException e) {}
		
		return false;
	}
	
	private Settings loadSettings() {
		Settings currentSettings = Settings.getSettings();
		if (currentSettings!=null) {

			Language currentLang = Language.getCurrentLanguage(this);
			for(int i=0; i<langAdapter.getCount(); i++) {
				if (langAdapter.getItem(i).getCode().equals(currentLang.getCode())) {
					languageSpinner.setSelection(i);
					break;
				}
			}			
			
			String username = currentSettings.getLogin();
			if (username == null) username = "";
			String password = currentSettings.getPassword();
			if (password == null) password = "";
			String serverAddr = currentSettings.getServerUrl();
			if (serverAddr == null) serverAddr = "";
			String serverEmailAddr = currentSettings.getC2dmServerMail();
			if (serverEmailAddr == null) serverEmailAddr = "";
			
			usernameField.setText(username);
			passwordField.setText(password);
			serverField.setText(serverAddr);
			c2dmServerMail.setText(serverEmailAddr);
			
			this.syncOnStartCb.setChecked(currentSettings.isSyncOnStart());
		}
		return currentSettings;
	}
	
	public void synchronize(View v) {
		SyncTask task = new SyncTask(this);
		task.execute();
		
	}
	
}
